<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addPower(1,'0')">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button v-if="row.type===1" size="small" type="text" @click="addPower(2,row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="edit(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <el-dialog :title="showTitle" :visible="showDialog" @close="handelClose">
      <!-- 表单内容 -->
      <el-form label-width="100px">
        <el-form-item label="权限名称">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="handelClose">取消</el-button>
          <el-button type="primary" @click="clickSubmit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { transListToTree } from '@/utils'
import { reqGetPermissionList, reqAddPermission, reqDelPermission, reqGetPermissionDetail, reqUpdatePermission } from '@/api/permisson'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑角色' : '添加角色'
    }
  },
  created() {
    this.GetPermissionList()
  },
  methods: {
    async GetPermissionList() {
      const { data } = await reqGetPermissionList()
      this.list = transListToTree(data, '0')
    },
    // 点击添加权限按钮，打开dialog
    addPower(type, pid) {
      this.formData.type = type
      this.formData.pid = pid
      this.showDialog = true
    },
    handelClose() {
      this.showDialog = false
      this.formData = {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
    },
    async clickSubmit() {
      if (this.formData.id) {
        await reqUpdatePermission(this.formData)
        this.$message.success('数据修改完成')
      } else {
        const res = await reqAddPermission(this.formData)
        this.$message.success(res.message)
      }
      this.handelClose()
      this.GetPermissionList()
    },
    // 删除角色
    async del(id) {
      const confir = await this.$confirm('您确定要删除该角色吗？').catch(err => err)
      if (confir === 'cancel') return
      await reqDelPermission(id)
      this.$message.success('删除角色成功')
      this.GetPermissionList()
    },
    // 查看角色
    async edit(id) {
      const { data } = await reqGetPermissionDetail(id)
      this.formData = data
      this.showDialog = true
    }

  }
}
</script>
